const DBUitls = require('../utils/DBUtils.js');

class BaseService extends DBUitls {
  constructor() {
    super();
    this.tableMap = {
      room_info: 'room_info',
      admin_info: 'admin_info',
      stu_info: 'stu_info',
      area_info: 't_area'
    };
    this.currentTableName = '';
    this.pageSize = 10;
  }

  /**
   * @description 根据一个主键id查询信息
   * @param {number} id 主键
   * @returns {Promise<object>} 查询结果
   */
  async findById(id) {
    let strSql = ` select * from ${this.currentTableName} where isDel = 0 and id = ? `;
    let results = await this.executeSql(strSql, [id]);
    return results[0];
  }

  /**
   * @description 根据一个主键删除信息
   * @param {number} id 主键
   * @returns {Promise<boolean>} true代表删除成功,false代表删除失败
   */
  async deleteById(id) {
    let strSql = ` update ${this.currentTableName} set isDel = true where id = ? `;
    let results = await this.executeSql(strSql, [id]);
    return results.affectedRows > 0 ? true : false;
  }

  /**
   * @description 获取所有数据
   * @returns {Promise<[]>} 返回查询结果
   */
  getAllList() {
    let strSql = ` select * from ${this.currentTableName} where isDel = 0 `;
    return this.executeSql(strSql);
  }
}

module.exports = BaseService;
